package edu.spec.pms.dao.impl;

import org.madgecko.basic.dao.BaseDao;
import edu.spec.pms.dao.iface.IRoleCommandDao;
import edu.spec.pms.model.sys.RoleCommand;
import org.springframework.stereotype.Repository;


@Repository
public class RoleCommandDao extends BaseDao<RoleCommand> implements IRoleCommandDao {

	@Override
	public RoleCommand loadByRoleAndCommand(int roleId, int commandId) {
		String hql = "select rc from RoleCommand rc left join fetch rc.role left join fetch rc.command where rc.role.id=? and rc.command.id=?";
		return (RoleCommand)this.getSession().createQuery(hql).setParameter(0, roleId).setParameter(1, commandId).uniqueResult();
	}

	@Override
	public void deleteByRoleAndCommand(int roleId, int command) {
		String hql = "delete RoleCommand rc where rc.role.id=? and rc.command.id=?";
		this.updateByHql(hql, new Object[]{roleId, command});
	}

	@Override
	public void deleteByRole(int roleId) {
		String hql = "delete RoleCommand rc where rc.role.id=?";
		this.updateByHql(hql, roleId);
	}

	@Override
	public void deleteByCommand(int commandId) {
		String hql = "delete RoleCommand rc where rc.command.id=?";
		this.updateByHql(hql, commandId);
	}
}
